﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HuabanAutoPost.Helper
{
    class WebHelper
    {
        /// <summary>
        /// 向豆瓣提交模拟数据
        /// </summary>
        /// <param name="PostData"></param>
        /// <param name="PostUrl"></param>
        /// <returns></returns>
        public static String PostTohuaban(NameValueCollection PostData,String PostUrl )
        {

            StaticValue.Client.Headers.Clear();
            StaticValue.Client.Encoding = Encoding.UTF8;

            StaticValue.Client.Headers.Add("Accept", "application/json");
            StaticValue.Client.Headers.Add("X-Requested-With", "XMLHttpRequest");
            StaticValue.Client.Headers.Add("X-Request", "JSON");

            StaticValue.Client.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko");
            StaticValue.Client.Headers.Add("Host", "huaban.com");
            try
            {
                byte[] responseArray = StaticValue.Client.UploadValues(PostUrl, "POST", PostData);

                // Decode and display the response.
                Console.WriteLine("\nResponse received was :\n{0}", Encoding.UTF8.GetString(responseArray));
                return Encoding.UTF8.GetString(responseArray);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return "";
            }


        }

        public static string GetHtml(String url)
        {
            StaticValue.Client.Headers.Clear();
            StaticValue.Client.Encoding = Encoding.UTF8;
         

            StaticValue.Client.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko");
            StaticValue.Client.Headers.Add("Host", new Uri(url).Host);
            string htmlSource ="";
            try
            {
                htmlSource = StaticValue.Client.DownloadString(url);
            }
            catch (Exception ex) {
                Console.WriteLine(ex);
            }

            return htmlSource;

        }
    }
}
